// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import AudioManage, { soundList } from "../../framework/AudioManage";
import mainManage from "../../framework/mainManage";
import sceneMediator from "../../framework/sceneMediator";
import Tool from "../../framework/Tool";
import logoSevenDayLayer from "../7day/logoSevenDayLayer";
import gameData, { TraverStatus } from "../gameData";
import randomData from "../mainRandomShowRoleSkin/randomData";


const {ccclass, property} = cc._decorator;

export enum mainPrefabView {
    EasterEggWallLayer = 0, //彩蛋界面
    CompensationLayer = 1, //补偿界面
    LuckyPlaneLayer = 2,  //幸运转盘
    InteriorTestLayer = 3, //内测员
    CatchDuckInquiryLayer = 4, //进入抓鸭子询问
    EnterCatchDuckLayer = 5, //进入抓鸭子界面
    CreateTavernLayer = 6, //创建酒馆界面
    EnterTavernLayer = 7, //进入酒馆界面
    SpringHappyLayer = 8, //春节狂欢
    SettingLayer = 9,   //设置界面
    ChooseLevelRank = 10, //普通关卡第一阶段
    NormalLevelLayer_1 = 11, //普通关卡第二阶段
    NormalLevelLayer_2 = 12, //普通关卡第三阶段
    SpecialLevelLayer = 13, //特殊关卡
    PetListLayer = 14, //宠物关卡
    SkinListLayer = 15, //宠物关卡
    BackHomeLayer = 16, //返场界面
    EnterEasterEggLevelLayer = 17, //是否进入彩蛋界面
    UserAgreementLayer = 18, //是否进入彩蛋界面
    randomShowRoleSkinLayer = 19, //随机使用人物皮肤
    logoSevenDayLayer = 20,  //累计登录

    //
    SkinPiece_0 = 30,
    SkinPiece_1 = 31,
    SkinPiece_2 = 32,
    SkinPiece_3 = 33,
    SkinPiece_4 = 34,
    SkinPiece_5 = 35,
}

@ccclass
export default class mainMediator extends cc.Component {

    @property(cc.Node)
    skinNode : cc.Node = null;

    @property(cc.Node)
    roleNode : cc.Node = null;

    @property(cc.SpriteFrame)
    picList : cc.SpriteFrame[] = [];

    static isLoading = false;

    clickTimes = 0;

    clickSkinTimes = 0;

    canClick = false;

    roleCameTime;
    onLoad () {

        //创建成功创建酒馆的消息传递
        cc.game.on("successCreateTavern",()=>{
            this.createTavernAction(TraverStatus.building, ()=>{

            });
        });

        //初始化bgm
        AudioManage.initBgmVolume();

        // if(gameData.unlockTavern){
        //     this.createTavernAction(TraverStatus.unBuild, ()=>{

        //     });
        // }else{
        //     this.createTavernAction(TraverStatus.open, ()=>{

        //     });
        // }

        // this.node.getChildByName("princeLevel").active = true;

        // if(gameData.colorfulPiece[0][1] == 0){
        //     this.node.getChildByName("level").on("touchstart",this.TouchStart,this);
        //     this.node.getChildByName("level").on("touchend",this.TouchEnd,this);
        //     this.node.getChildByName("level").on("touchcancel",this.TouchEnd,this);
        // }else{
        //     this.node.getChildByName("level").getComponent(cc.Button).enabled = true;
        // }

        // if(gameData.getRoleProcessSkin()[11] == 1){
        //     this.skinNode.active = false;
        // }

        // this.mainSceneAnim();

        this.schedule(this.checkRandomRoleSkin,1);
        logoSevenDayLayer.getLoginDay();
        logoSevenDayLayer.getLoginTime();
        logoSevenDayLayer.getNowTimes();
        this.scheduleOnce(()=>{
            if((logoSevenDayLayer.nowTimesNum - logoSevenDayLayer.oldTimesNum) / 86400 >= 1 && logoSevenDayLayer.loginDayNum < 7)
            {
                logoSevenDayLayer.isGetValue = 0;
                logoSevenDayLayer.saveAward();
                logoSevenDayLayer.saveLoginTime();
                this.showPrefabLayer(mainPrefabView.logoSevenDayLayer, ()=>{
    
                });
            }
        },0.5)

    }

    mainSceneAnim(){
        this.node.getChildByName("start").active = false;
        this.node.getChildByName("start").scale = 3;

        cc.tween(this.node.getChildByName("skin"))
            .by(0.2,{position:cc.v2(0,800)})
            .start();
        cc.tween(this.node.getChildByName("pet"))
            .by(0.2,{position:cc.v2(0,800)})
            .start();
        this.scheduleOnce(()=>{
            // cc.tween(this.node.getChildByName("princeLevel"))
            //     .by(0.2,{position:cc.v2(0,600)})
            //     .start();
            // this.scheduleOnce(()=>{
                
            // },0.06)
            cc.tween(this.node.getChildByName("level_0"))
                    .by(0.2,{position:cc.v2(0,800)})
                    .start()
                    this.scheduleOnce(()=>{
                        cc.tween(this.node.getChildByName("level_1"))
                            .by(0.2,{position:cc.v2(0,800)})
                            .start()
                            this.scheduleOnce(()=>{
                                cc.tween(this.node.getChildByName("level_2"))
                                    .by(0.2,{position:cc.v2(0,800)})
                                    .start()
                            },0.06)
                    },0.06)
        },0.06);

        cc.tween(this.node.getChildByName("eggWall"))
            .by(0.2,{position:cc.v2(-800,0)})
            .start();
        this.scheduleOnce(()=>{
            cc.tween(this.node.getChildByName("compensation"))
                .by(0.2,{position:cc.v2(-800,0)})
                .start();
            this.scheduleOnce(()=>{
                cc.tween(this.node.getChildByName("EasterEgg"))
                    .by(0.2,{position:cc.v2(-800,0)})
                    .start();
                this.scheduleOnce(()=>{
                    cc.tween(this.node.getChildByName("desktop"))
                        .by(0.2,{position:cc.v2(-800,0)})
                        .start();
                },0.06)
            },0.06)
        },0.06);

        cc.tween(this.node.getChildByName("reward"))
            .by(0.2,{position:cc.v2(-800,0)})
            .start();
        this.scheduleOnce(()=>{
            cc.tween(this.node.getChildByName("testPlayer"))
                .by(0.2,{position:cc.v2(-800,0)})
                .start();
            this.scheduleOnce(()=>{
                cc.tween(this.node.getChildByName("duckEnter"))
                    .by(0.2,{position:cc.v2(-800,0)})
                    .start();
                this.scheduleOnce(()=>{
                    cc.tween(this.node.getChildByName("rebackBtn"))
                        .by(0.2,{position:cc.v2(-800,0)})
                        .start();
                },0.06)
            },0.06)
        },0.06);


        cc.tween(this.node.getChildByName("logo-hwcw"))
            .by(0.2,{position:cc.v2(0,-300)})
            .by(0.1,{position:cc.v2(0,80)})
            .by(0.08,{position:cc.v2(0,-80)})
            .start();
        cc.tween(this.node.getChildByName("handSwitch"))
            .by(0.2,{position:cc.v2(0,-300)})
            .by(0.1,{position:cc.v2(0,80)})
            .by(0.08,{position:cc.v2(0,-80)})
            .start();

        this.scheduleOnce(()=>{
            cc.tween(this.node.getChildByName("settingTarget").getChildByName("setting"))
                .to(0.2,{position:cc.v2(0,0)})
                .start();
            cc.tween(this.node.getChildByName("settingTarget").getChildByName("reset"))
                .to(0.2,{position:cc.v2(100,0)})
                .start();
            this.node.getChildByName("start").active = true;
            cc.tween(this.node.getChildByName("start"))
                .to(0.2,{scale:1})
                .start();
        },0.3)
    }

    TouchStart(event){
        this.indexOrder = 0;

        if(gameData.colorfulPiece[0][1] == 0){
            this.schedule(this.showSkinPiece,1);
        }

        this.node.getChildByName("level").scale = 1;
    }

    indexOrder = 0;
    shakeAction = null;
    hatShake(){
        cc.tween(this.node.getChildByName("level").getChildByName("hat")).by(0.1,{
            position : cc.v2(0,10)
        }).call(()=>{
            cc.tween(this.node.getChildByName("level").getChildByName("hat")).by(0.1,{
                position : cc.v2(0,-10)
            }).start();
        }).start()
    }

    showSkinPiece(){
        this.indexOrder++;
        if(this.indexOrder == 1){
            this.schedule(this.hatShake,0.2);
        }
        if(this.indexOrder == 5){
            this.unschedule(this.hatShake);
            this.unschedule(this.showSkinPiece);
            gameData.colorfulPiece[0][1] = 1;
            gameData.saveColorfulPiece();
            this.node.getChildByName("level").getChildByName("piece2").active = true;
            cc.tween(this.node.getChildByName("level").getChildByName("hat")).by(0.5,{
                position : cc.v2(0,-cc.winSize.height)
            }).call(()=>{
                this.node.getChildByName("level").getChildByName("hat").y += (cc.winSize.height -10);
            }).start();
            let pos = cc.find("Canvas").convertToWorldSpaceAR(cc.v2(0,0));
            let pos2 = this.node.getChildByName("level").convertToNodeSpaceAR(pos);
            cc.tween(this.node.getChildByName("level").getChildByName("piece2")).to(0.5,{
                position : cc.v2(pos2.x,pos2.y)
            }).call(()=>{
                this.node.getChildByName("level").getChildByName("piece2").active = false;
                Tool.loadPrefab("prefabs/getSkin",res=>{
                    let view = cc.instantiate(res);
                    if(gameData.getColorfulPieceNumber(gameData.colorfulPiece[0]) == 7){
                        view.getComponent("getSkinMediator").setCallBack(true,2);
                    }else{
                        view.getComponent("getSkinMediator").setCallBack(false,2);
                    }
                    view.parent = cc.find("Canvas");
                });
                console.log("展示碎片");
            }).start()
        }
    }

    TouchEnd(event){
        if(this.indexOrder < 5){
            this.unschedule(this.showSkinPiece);
            this.unschedule(this.hatShake);
            this.unschedule(this.showSkinPiece);
            AudioManage.playSFX(soundList.uiClick,false);
            Tool.loadPrefab("mainPrefab/levelView",(res)=>{
                let view = cc.instantiate(res);
                view.parent = cc.find("Canvas");
            });
            console.log("关卡");
        }
        this.node.getChildByName("level").scale = 0.9;
    }

    start(){

        this.roleNode.getChildByName("pic").active = false;

        let curSkin = -1;
        if(gameData.randomSkinIndex != -1){
            if(randomData.isUseRandomSkin()){
                curSkin = gameData.randomSkinIndex;
            }else{
                if(gameData.getPetProcessSkin()[gameData.randomSkinIndex] == 0){
                    curSkin = gameData.randomSkinIndex;
                }else{
                    curSkin = gameData.getCurUseRoleSkin();
                }
            }
        }else{
            curSkin = gameData.getCurUseRoleSkin();
        }
        console.log(curSkin);
        //设置人物的皮肤
        if(curSkin < 9){
            cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
                if(err){
                    console.log(err);
                    return
                }
                let spk = this.roleNode.getComponent(sp.Skeleton);
                spk.skeletonData = res;
                spk.setSkin("pf0" +  (curSkin + 1));
                this.roleCameIn(spk);
                // spk.setAnimation(0,"animation",true);
            });
        }else{
            if(curSkin == 9){
                cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
                    if(err){
                        console.log(err);
                        return
                    }
                    let spk = this.roleNode.getComponent(sp.Skeleton);
                    spk.skeletonData = res;
                    spk.setSkin("pf" +  (curSkin + 1));
                    this.roleCameIn(spk);
                    // spk.setAnimation(0,"animation",true);
                });
            }else if(curSkin == 10){
                cc.loader.loadRes("anim/zhujue19",sp.SkeletonData,(err,res)=>{
                    if(err){
                        console.log(err);
                        return
                    }
                    let spk = this.roleNode.getComponent(sp.Skeleton);
                    spk.skeletonData = res;
                    spk.setSkin("pf1");
                    this.roleCameIn(spk);
                    // spk.setAnimation(0,"animation",true);
                });
            }else if(curSkin == 11){
                cc.loader.loadRes("anim/zhujue16",sp.SkeletonData,(err,res)=>{
                    if(err){
                        console.log(err);
                        return
                    }
                    let spk = this.roleNode.getComponent(sp.Skeleton);
                    spk.skeletonData = res;
                    spk.setSkin("pf1");
                    this.roleCameIn(spk);
                    // spk.setAnimation(0,"animation",true);
                });
            }else if(curSkin == 12){
                cc.loader.loadRes("anim/zhujue19",sp.SkeletonData,(err,res)=>{
                    if(err){
                        console.log(err);
                        return
                    }
                    let spk = this.roleNode.getComponent(sp.Skeleton);
                    spk.skeletonData = res;
                    spk.setSkin("pf01");
                    this.roleCameIn(spk);
                    // spk.setAnimation(0,"animation",true);
                }); 
            }
            // if(curSkin == 20){
            //     if(gameData.lastColorfulList[2] == 0){
            //         cc.loader.loadRes("anim/zhujue22_1",sp.SkeletonData,(err,res)=>{
            //             if(err){
            //                 console.log(err);
            //                 return
            //             }
            //             let spk = this.roleNode.getComponent(sp.Skeleton);
            //             spk.skeletonData = res;
            //             this.roleCameIn(spk);
            //             // spk.setAnimation(0,"animation",true);
            //         })
            //     }else if(gameData.lastColorfulList[2] == 1){
            //         cc.loader.loadRes("anim/zhujue22_2",sp.SkeletonData,(err,res)=>{
            //             if(err){
            //                 console.log(err);
            //                 return
            //             }
            //             let spk = this.roleNode.getComponent(sp.Skeleton);
            //             spk.skeletonData = res;
            //             this.roleCameIn(spk);
            //             // spk.setAnimation(0,"animation",true);
            //         })
            //     }
            // }else if(curSkin >= 12){
            //     cc.loader.loadRes("anim/zhujue" + (curSkin + 2),sp.SkeletonData,(err,res)=>{
            //         if(err){
            //             console.log(err);
            //             return
            //         }
            //         let spk = this.roleNode.getComponent(sp.Skeleton);
            //         spk.skeletonData = res;
            //         if(curSkin == 12){
            //             spk.setSkin("pfi" + (gameData.lastColorfulPart + 1));
            //         }else if(curSkin == 14){
            //             spk.setSkin("pf1");
            //         }else if(curSkin == 16){
            //             spk.setSkin("pifu" + (gameData.lastColorfulList[1] + 1));
            //         }else if(curSkin == 17){
            //             spk.setSkin("pf01");
            //         }
            //         this.roleCameIn(spk);
            //         // spk.setAnimation(0,"animation",true);
            //     })
            // }else{
            //     cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
            //         if(err){
            //             console.log(err);
            //             return
            //         }
            //         let spk = this.roleNode.getComponent(sp.Skeleton);
            //         spk.skeletonData = res;
            //         spk.setSkin("pf" +  (curSkin + 1));
            //         this.roleCameIn(spk);
            //         // spk.setAnimation(0,"animation",true);
            //     });
            // }
        }

        cc.game.on("resetRoleSkin",()=>{

            let curSkin = -1;
            if(gameData.randomSkinIndex != -1){
                if(randomData.isUseRandomSkin()){
                    curSkin = gameData.randomSkinIndex;
                }else{
                    if(gameData.getPetProcessSkin()[gameData.randomSkinIndex] == 0){
                        curSkin = gameData.randomSkinIndex;
                    }else{
                        curSkin = gameData.getCurUseRoleSkin();
                    }
                }
            }else{
                curSkin = gameData.getCurUseRoleSkin();
            }

            console.log(curSkin);

            if(curSkin < 9){
                cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
                    if(err){
                        console.log(err);
                        return
                    }
                    let spk = this.roleNode.getComponent(sp.Skeleton);
                    spk.skeletonData = res;
                    spk.setSkin("pf0" +  (curSkin + 1));
                    // this.roleCameIn(spk);
                    spk.setAnimation(0,"animation",true);
                });
            }else{
                // if(curSkin == 20){
                //     if(gameData.lastColorfulList[2] == 0){
                //         cc.loader.loadRes("anim/zhujue22_1",sp.SkeletonData,(err,res)=>{
                //             if(err){
                //                 console.log(err);
                //                 return
                //             }
                //             let spk = this.roleNode.getComponent(sp.Skeleton);
                //             spk.skeletonData = res;
                //             spk.setAnimation(0,"animation",true);
                //         })
                //     }else if(gameData.lastColorfulList[2] == 1){
                //         cc.loader.loadRes("anim/zhujue22_2",sp.SkeletonData,(err,res)=>{
                //             if(err){
                //                 console.log(err);
                //                 return
                //             }
                //             let spk = this.roleNode.getComponent(sp.Skeleton);
                //             spk.skeletonData = res;
                //             spk.setAnimation(0,"animation",true);
                //         })
                //     }
                // }else if(curSkin >= 12){
                //     cc.loader.loadRes("anim/zhujue" + (curSkin + 2),sp.SkeletonData,(err,res)=>{
                //         if(err){
                //             console.log(err);
                //             return
                //         }
                //         let spk = this.roleNode.getComponent(sp.Skeleton);
                //         spk.skeletonData = res;
                //         if(curSkin == 12){
                //             spk.setSkin("pfi" + (gameData.lastColorfulPart + 1));
                //         }else if(curSkin == 14){
                //             spk.setSkin("pf1");
                //         }else if(curSkin == 16){
                //             spk.setSkin("pifu" + (gameData.lastColorfulList[1] + 1));
                //         }
                //         // this.roleCameIn(spk);
                //         spk.setAnimation(0,"animation",true);
                //     })
                // }else{
                //     cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
                //         if(err){
                //             console.log(err);
                //             return
                //         }
                //         let spk = this.roleNode.getComponent(sp.Skeleton);
                //         spk.skeletonData = res;
                //         spk.setSkin("pf" +  (curSkin + 1));
                //         // this.roleCameIn(spk);
                //         spk.setAnimation(0,"animation",true);
                //     });
                // }
                if(curSkin == 9){
                    cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let spk = this.roleNode.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setSkin("pf" +  (curSkin + 1));
                        spk.setAnimation(0,"animation",true);
                    });
                }else if(curSkin == 10){
                    cc.loader.loadRes("anim/zhujue19",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let spk = this.roleNode.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setSkin("pf1");
                        spk.setAnimation(0,"animation",true);
                    });
                }else if(curSkin == 11){
                    cc.loader.loadRes("anim/zhujue16",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let spk = this.roleNode.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setSkin("pf1");
                        spk.setAnimation(0,"animation",true);
                    });
                }else if(curSkin == 12){
                    cc.loader.loadRes("anim/zhujue19",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let spk = this.roleNode.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setSkin("pf01");
                        spk.setAnimation(0,"animation",true);
                    });
                }
            }
        });

        if(!mainMediator.isLoading){
            mainMediator.isLoading = true;
            cc.loader.loadRes("sound/bg",cc.AudioClip,(completedCount: number, totalCount: number, item: any)=>{
                
            },(error, resource) => {
                if(error){
                    console.log("加载音频资源==>",error);
                    return;
                }
                console.log("=====>音频资源加载完毕<=====");
                AudioManage.playBGM(soundList.musicMenu);
            });

            //春节大放送
            // let month = new Date().getMonth() + 1;
            // let day = new Date().getDate();
            // if(month == 2 && day >= 7 && day <= 16){
            //     Tool.loadPrefab("mainPrefab/newYearDay",(res)=>{
            //         let view = cc.instantiate(res);
            //         view.parent = cc.find("Canvas");
            //     });
            // }
        }

        if(gameData.returnPrivacyAgreementInformation() == 0){
            gameData.savePrivacyAgreementInformation(1);
            this.showPrefabLayer(mainPrefabView.UserAgreementLayer, ()=>{
                
            });
        }
    }

    checkRandomRoleSkin(){
        if(!randomData.isUseRandomSkin() && gameData.getPetProcessSkin()[gameData.randomSkinIndex] == 0){
            cc.game.emit("resetRoleSkin");
        }
    }

    roleCameIn(spk){
        if(gameData.curUseRoleSkin == 16 && gameData.lastColorfulList[1] == 1){
            spk.setAnimation(0,"run1",true);
        }else{
            spk.setAnimation(0,"run",true);
        }
        let rolePos = this.node.getChildByName("bg").getChildByName("roleTarget").position;
        let roleParent = this.node.getChildByName("bg").getChildByName("rolePar");
        cc.tween(roleParent)
            .to((rolePos.x -  roleParent.x) / 500,{position:cc.v2(rolePos)})
            .call(()=>{
                spk.setAnimation(0,"animation",true);
            })
            .start();

        let petGroup = this.node.getChildByName("bg").getChildByName("petGroup").children;
        let petRoot = this.node.getChildByName("bg").getChildByName("petTarget").children;
        for (let i in petRoot){
            cc.tween(petGroup[i])
                .to((petRoot[i].x - petGroup[i].x) / 500,{position:cc.v2(petRoot[i].position)})
                .start()
        }

        console.log("人物进场时间：",(rolePos.x -  roleParent.x) / 500);
    }


    protected onDestroy(): void {
        cc.game.off("resetRoleSkin");
        cc.game.off("resetAdvenTitle");

        this.unschedule(this.checkRandomRoleSkin);
    }

    //彩蛋按钮
    EggBtnScale(){
        cc.tween(this.node.getChildByName("EasterEgg")).to(0.2,{
            scale : 1.45
        }).call(()=>{
            cc.tween(this.node.getChildByName("EasterEgg")).to(0.2,{
                scale : 1.3
            }).call(()=>{
                cc.tween(this.node.getChildByName("EasterEgg")).delay(0.3).call(()=>{
                    this.EggBtnScale();
                }).start();
            }).start();
        }).start();
    }

    clearLocalData(){

        sceneMediator.princeShowLevel = 0;
        sceneMediator.princeHighLevel = 0;
        sceneMediator.showLevel = 0;
        sceneMediator.highLevel = 0;
        sceneMediator.saveLevelData();
        sceneMediator.saveHighLevel();

        gameData.showRewardApple  = false;

        gameData.showConversation = false;
        gameData.unlockPrinceStone5 = false;
        gameData.savePrinceLevelLocalPart1();

        gameData.colorfulPiece = [
            [0,0,0,0,0,0,0],
            [0,0,0,0,0]
        ];
        gameData.saveColorfulPiece();

        gameData.lastColorfulPart = 0;
        gameData.saveLastColorfulPart();


        gameData.pumpkinShowDate = [];
        for(let i in gameData.levelPumpkinDate){
            let arr1 = [];
            for(let j = 0; j < gameData.levelPumpkinDate[i];j++){
                arr1.push(1);
            }
            gameData.pumpkinShowDate.push(arr1);
        }
        gameData.savePumpkinShowDate();

        gameData.curUseSkin = [];
        gameData.saveLastPetSkin();

        gameData.petSkinList = [];
        for(let i = 0; i < 17;i++){
            gameData.petSkinList.push(0);
        }
        Tool.saveData("petSkin",JSON.stringify(gameData.petSkinList));

        gameData.roleSkinList = [1];
        for(let i = 0; i < 15;i++){
            gameData.roleSkinList.push(0);
        }
        Tool.saveData("roleSkin",JSON.stringify(gameData.roleSkinList));

        gameData.curUseRoleSkin = 0;
        gameData.saveLastRoleSkin();


        gameData.processPumpkinDate = [];
        for(let i = 0 ; i < 60 ;i++){
            gameData.processPumpkinDate.push(0);
        }
        gameData.savePumpkinDate();

        gameData.processPumpkinNumber = 0;
        gameData.savePumpkinNumber();

        gameData.eggSkinList = [0,0];
        gameData.saveEggSkinList();

        gameData.seeVideoTimes = [0,0,0];
        gameData.saveSeeVideoTimes();

        gameData.skinThreadLockData = [];
        for(let i in gameData.skinWallDate){
            let arr = [];
            let number = gameData.skinWallDate[i]["keynum"];
            for(let j = 0 ; j < number ;j++){
                arr.push(0);
            }
            gameData.skinThreadLockData.push(arr);
        }
        gameData.saveGetSkinThreadLockData();

        gameData.eggThreadLockData = [];
        for(let i in gameData.eggWallDate){
            let arr = [];
            let number = gameData.eggWallDate[i]["keynum"];
            for(let j = 0 ; j < number ;j++){
                arr.push(0);
            }
            gameData.eggThreadLockData.push(arr);
        }
        gameData.saveGetEggThreadLockData();


        cc.director.loadScene("main");
    }


    clickList = [false,false,false,false,false,false,false,false,false,false,false,false];
    clickHatTimes = 0;
    onClick(event,data){

        AudioManage.playSFX(soundList.uiClick,false);
        if(parseInt(data) == 1){
            //设置界面
            this.showPrefabLayer(mainPrefabView.SettingLayer, ()=>{

            });
        }
        else if(parseInt(data) == 2){
            // //普通关卡界面
            // this.showPrefabLayer(mainPrefabView.NormalLevelLayer_0, ()=>{

            // });
            console.log("关卡");
        }
        else if(parseInt(data) == 3){
            //普通关卡界面
            this.showPrefabLayer(mainPrefabView.NormalLevelLayer_1, ()=>{

            });
            console.log("关卡");
        }
        else if(parseInt(data) == 4){
            //普通关卡界面
            this.showPrefabLayer(mainPrefabView.NormalLevelLayer_2, ()=>{

            });
            console.log("关卡");
        }
        else if(parseInt(data) == 5){
            //宠物列表界面
            this.showPrefabLayer(mainPrefabView.PetListLayer, ()=>{

            });
        }
        else if(parseInt(data) == 6){
            //人物列表界面
            this.showPrefabLayer(mainPrefabView.SkinListLayer, ()=>{

            });
        }
        else if(parseInt(data) == 7){
            console.log("开始游戏");
            // AudioManage.stopBGM();
            // sceneMediator.LoadCurScene();
            this.showPrefabLayer(mainPrefabView.ChooseLevelRank, ()=>{

            });
        }
        else if(parseInt(data) == 8){

            //是否已经玩过彩蛋界面 没玩进入特殊关卡玩
            if(gameData.passEasterEgg){
                gameData.passEasterEgg = false;
                gameData.saveEasterEgg();
                AudioManage.stopBGM();
                cc.director.loadScene("sp1");
            }
        }
        else if(parseInt(data) == 9){

            //点击小鹿的腿 触发彩蛋环节 获取皮肤
            if(this.canClick) return;
            if(gameData.getRoleProcessSkin()[11] == 1 || this.clickSkinTimes > 7) return;
            this.canClick = true;
            this.clickSkinTimes++;
            cc.tween(this.skinNode.getChildByName("pic"))
                .to(0.1,{scale : 1.2})
                .to(0.1,{scale : 1})
                .call(  ()=>{
                    this.canClick = false;
                })
                .start();

            if(this.clickSkinTimes > 7){
                Tool.loadPrefab("prefabs/eggSkin",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("eggSkin").setCallBack(11,0,(res)=>{
                        if(res){
                            this.skinNode.active = false;
                        }else{
                            this.clickSkinTimes = 0;
                        }
                    });
                    view.parent = cc.find("Canvas");
                })
            }
        }
        else if(parseInt(data) == 10){
            //进入补偿福利界面
            this.showPrefabLayer(mainPrefabView.CompensationLayer, ()=>{

            })
        }
        else if(parseInt(data) == 11){
            //进入彩蛋墙界面
            this.showPrefabLayer(mainPrefabView.EasterEggWallLayer, ()=>{
                if (Tool.getData("rebackIsSuccess") == 0){
                    this.node.getChildByName("rebackBtn").active = true;
                }else {
                    this.node.getChildByName("rebackBtn").active = false;
                }
            });
        }
        else if(parseInt(data) == 12){
            //进入特殊关卡界面
            this.showPrefabLayer(mainPrefabView.SpecialLevelLayer, ()=>{

            })
        }
        else if(parseInt(data) == 13){
            //幸运转盘
            this.showPrefabLayer(mainPrefabView.LuckyPlaneLayer, ()=>{

            })
        }
        else if (parseInt(data) == 14){
            //小黄鸭询问入口
            this.showPrefabLayer(mainPrefabView.CatchDuckInquiryLayer, ()=>{

            })
        }
        else if (parseInt(data) == 15){
           //判断是否可以创建酒馆
            gameData.judgeTimeCome(7,()=>{
                AudioManage.stopBGM();
                if(gameData.unlockTavern){
                    // cc.director.loadScene("drunkery");
                }else{
                    //弹出是否可以创建酒馆
                    this.showPrefabLayer(mainPrefabView.CreateTavernLayer, ()=>{

                    });
                }
            }, ()=>{
                mainManage.createToast("等到2月7日即可建造酒馆！")
            });
        }
        else if (parseInt(data) == 16){
           //内测员界面
           this.showPrefabLayer(mainPrefabView.InteriorTestLayer, ()=>{

            })
        }
        else if (parseInt(data) == 17){
            this.showPrefabLayer(mainPrefabView.randomShowRoleSkinLayer, ()=>{

            });
        }
        else if (parseInt(data) == 18){
            this.showPrefabLayer(mainPrefabView.logoSevenDayLayer, ()=>{

            });
        }
    }

    //创建酒馆的动画过程
    createTavernAction(Status : TraverStatus,callback){

        let node = this.node.getChildByName("bg").getChildByName("tavern");
        let build = node.getChildByName("build").getComponent(sp.Skeleton);
        let spk = node.getChildByName("spine").getComponent(sp.Skeleton);

        switch (Status) {
            case TraverStatus.unBuild :
                //还没有建立酒馆
                spk.setAnimation(0,"animation",false);
                spk.setCompleteListener(()=>{
                    spk.setAnimation(0,"animation2",true);
                    spk.setCompleteListener(()=>{
                        callback();
                    });
                });
                break;
            case TraverStatus.building :
                //正在建立酒馆
                spk.node.active = false;
                build.setAnimation(0,"animation",false);
                build.setCompleteListener(()=>{
                    build.node.active = false;
                    spk.node.active = true;
                    spk.setAnimation(0,"animation",false);
                    spk.setCompleteListener(()=>{
                        spk.setAnimation(0,"animation2",true);
                        spk.setCompleteListener(()=>{

                        });
                    });
                    gameData.unlockTavern = true;
                    gameData.saveOtherGameData();
                    mainManage.createToast("成功建造出了温泉酒馆!");
                    callback();
                });
                break;
            case TraverStatus.open :
                //酒馆处于开放状态
                spk.setAnimation(0,"animation3",true);
                callback();
                break;
            case TraverStatus.close :
                //酒馆处于关闭状态
                break;
            default :
                break;
        }
    }



    //展示预制界面
    showPrefabLayer(layer : mainPrefabView,callback){
        switch (layer) {
            //返场界面---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.BackHomeLayer :
                Tool.loadPrefab("mainPrefab/skinBackLayer",(res)=>{
                    let view = cc.instantiate(res);
                    view.parent = cc.find("Canvas");
                    callback();
                });
                break;
            //彩蛋墙界面---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.EasterEggWallLayer :
                Tool.loadPrefab("mainPrefab/eggWall",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("eggWallMediator").setCallBack(()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                    callback();
                });
                break;

            //彩蛋界面---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.EnterEasterEggLevelLayer :
                Tool.loadPrefab("mainPrefab/easterEggView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("easterEggWindow").setShowInit(1,()=>{
                        callback(true)
                    },-1,()=>{
                        callback(false)
                    });
                    view.parent = cc.find("Canvas");
                });
                break;

            //补偿界面---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.CompensationLayer :
                Tool.loadPrefab("prefabs/compensation",res=>{
                    let view = cc.instantiate(res);
                    view.getComponent("compensationMediator").setCallBack(true,()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
            //幸运转盘---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.LuckyPlaneLayer :
                AudioManage.stopBGM();
                Tool.loadPrefab("mainPrefab/turnplateView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("turnplateView").setCallback(()=>{
                        AudioManage.playBGM(soundList.musicMenu);
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
            //抓鸭子询问---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.CatchDuckInquiryLayer :
                Tool.loadPrefab("mainPrefab/enterDuckLayer",(res)=>{
                    let view = cc.instantiate(res);
                    view.parent = cc.find("Canvas");
                    callback();
                });
                break;
            //创建酒馆询问---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.CreateTavernLayer :
                Tool.loadPrefab("mainPrefab/createTavernLayer",(res)=>{
                    let view = cc.instantiate(res);
                    view.parent = cc.find("Canvas");
                    callback();
                });
                break;
            //春节狂欢---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.SpringHappyLayer :
                Tool.loadPrefab("mainPrefab/newYearDay",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("newYearDay").setCallBack(()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                callback();
                break;
            //进入内测员---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.InteriorTestLayer :
                AudioManage.stopBGM();
             
                break;
            //宠物界面 ---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.PetListLayer :
                Tool.loadPrefab("mainPrefab/petView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("petViewMediator").setCallBack(()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
            //皮肤界面 ---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.SkinListLayer :
                Tool.loadPrefab("mainPrefab/skinView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("skinViewMediator").setCloseBack(()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
            //设置界面 ---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.SettingLayer :
                Tool.loadPrefab("mainPrefab/settingView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("settingView").setCallBack(()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
            //普通关卡界面 ---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.ChooseLevelRank :
                
                Tool.loadPrefab("mainPrefab/chooseLevelRank",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("chooseLevelRank").setCallBack(()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
                //普通关卡界面 ---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.NormalLevelLayer_1 :
                Tool.loadPrefab("mainPrefab/levelView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("levelViewMediator").setCallBack(1,()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
                //普通关卡界面 ---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.NormalLevelLayer_2 :
                Tool.loadPrefab("mainPrefab/levelView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("levelViewMediator").setCallBack(2,()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
            case mainPrefabView.SpecialLevelLayer :
                Tool.loadPrefab("mainPrefab/princeLevelView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("princeLevelViewMediator").setCallBack(()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
            case mainPrefabView.UserAgreementLayer :
                Tool.loadPrefab("mainPrefab/userAgreement",(res)=>{
                    let view = cc.instantiate(res);
                    view.parent = cc.find("Canvas");
                    callback();
                });
                break;
            case mainPrefabView.randomShowRoleSkinLayer :
                Tool.loadPrefab("mainPrefab/randomShowRoleSkin",(res)=>{
                    let view = cc.instantiate(res);
                    view.parent = cc.find("Canvas");
                    callback();
                });
                break;

            case mainPrefabView.logoSevenDayLayer :
                Tool.loadPrefab("mainPrefab/logoSevenDayLayer",(res)=>{
                    let view = cc.instantiate(res);
                    view.parent = cc.find("Canvas");
                    callback();
                });
                break;    
            default:
                break;

        }
    }

    // update (dt) {}
}
